//
//  UIButton+Extension.m
//  分类
//
//  Created by 裘明 on 15/12/14.
//  Copyright © 2015年 裘明. All rights reserved.
//

#import "UIButton+Extension.h"
#import <objc/runtime.h>

/// 标题默认颜色
#define kItemTitleColor ([UIColor colorWithWhite:80.0 / 255.0 alpha:1.0])
/// 标题高亮颜色
#define kItemTitleHighlightedColor ([UIColor orangeColor])
/// 标题字体大小
#define kItemFontSize   14


@implementation UIButton (Extension)

@dynamic info_Dict;
static const void *IndieBandNameKey = &IndieBandNameKey;

#pragma mark 快速创建纯色按钮
+ (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)titleColor bgColor:(UIColor *)bgColor {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.backgroundColor = bgColor;
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    return btn;
}

#pragma mark  给按钮设置内容图片  图片按钮 imgName
- (void)setContentImageName:(NSString *)imgName {
    if (imgName) {
        UIImage *img = [UIImage imageNamed:imgName];
        [self setContentImage:img];
    }
    
}

+ (instancetype)setBtnWithImageName:(NSString *)imageName BackgroundImageName:(NSString *)backgroundImageName {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    NSString *highlight = [NSString stringWithFormat:@"%@_highlighted", imageName];
    [btn setImage:[UIImage imageNamed:highlight] forState:UIControlStateHighlighted];
    
    [btn setBackgroundImage:[UIImage imageNamed:backgroundImageName] forState:UIControlStateNormal];
    
    NSString *bgHighlight = [NSString stringWithFormat:@"%@_highlighted", backgroundImageName];
    [btn setBackgroundImage:[UIImage imageNamed:bgHighlight] forState:UIControlStateHighlighted];
    return btn;
}

#pragma mark  给按钮设置内容图片  图片按钮 img
- (void)setContentImage:(UIImage *)img {
    if (img) {
        UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
        CGSize selfSize = self.frame.size;
        CGSize imgSize = img.size;
        float imgParm_float = imgSize.width / imgSize.height;
        if (selfSize.width < imgSize.width) {
            imgSize.width = selfSize.width;
            imgSize.height = imgSize.width / imgParm_float;
        }
        
        if (selfSize.height < imgSize.height) {
            imgSize.height = selfSize.height;
            imgSize.width = imgSize.height * imgParm_float;
        }
        
        imgView.frame = CGRectMake((selfSize.width - imgSize.width) / 2, (selfSize.height - imgSize.height) / 2, imgSize.width, imgSize.height);
        [self addSubview:imgView];
    }
}

#pragma mark创建确定 重置按钮
+ (UIButton *)creadBtn:(NSString *)titleStr color:(UIColor *)color
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    btn.backgroundColor = color;
    [btn setTitle:titleStr forState:UIControlStateNormal];
    return  btn;
}

#pragma mark 使用图像名创建Btn
+ (instancetype)buttonWithTitle:(NSString *)title imageName:(NSString *)imageName target:(id)target action:(SEL)action {
    
    UIButton *button = [[self alloc] init];
    
    // 设置图像
    if (imageName != nil) {
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        
        NSString *highlighted = [NSString stringWithFormat:@"%@_highlighted", imageName];
        [button setImage:[UIImage imageNamed:highlighted] forState:UIControlStateHighlighted];
    }
    
    // 设置标题
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:kItemTitleColor forState:UIControlStateNormal];
    [button setTitleColor:kItemTitleHighlightedColor forState:UIControlStateHighlighted];
    button.titleLabel.font = [UIFont systemFontOfSize:kItemFontSize];
    
    [button sizeToFit];
    
    // 监听方法
    if (action != nil) {
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    return button;
}

#pragma mark 创建按钮设置标题、标题颜色、背景图片(普通)
+ (instancetype)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor bgImageName:(NSString *)imageName {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn sizeToFit];
    
    return btn;
}

+ (instancetype)buttonWithTitle:(NSString *)title color:(UIColor *)color fontSize:(CGFloat)fontSize imageName:(NSString *)imageName backImageName:(NSString *)backImageName {
    
    UIButton *button = [[UIButton alloc] init];
    
    // 设置标题
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    
    // 图片
    if (imageName != nil) {
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        
        NSString *highlighted = [NSString stringWithFormat:@"%@_highlighted", imageName];
        [button setImage:[UIImage imageNamed:highlighted] forState:UIControlStateHighlighted];
    }
    
    // 背景图片
    if (backImageName != nil) {
        [button setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
        
        NSString *backHighlighted = [NSString stringWithFormat:@"%@_highlighted", backImageName];
        [button setBackgroundImage:[UIImage imageNamed:backHighlighted] forState:UIControlStateHighlighted];
    }
    
    return button;
}

#pragma mark 按钮添加点击方法
- (void)addTarget:(id)target action:(SEL)action {
    [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

+ (instancetype)btnWithTitle:(NSString *)title titleColor:(UIColor *)titleColor backgroundImage:(NSString *)imageName {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    return btn;
}


#pragma mark 详细属性设置
- (void)setInfo_Dict:(NSDictionary *)info_Dict {
    objc_setAssociatedObject(self, IndieBandNameKey, info_Dict, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSDictionary *)info_Dict {
    return objc_getAssociatedObject(self, IndieBandNameKey);
}

@end
